﻿<html>

<head>
</head>

<?php

	if($_POST){
		$idPost = $_POST["optTitulo"];
		$ultimoId = 1;
		$usuario = 1;
		$conteudo = $_POST["txtComentario"];
		$data = date('d/m/Y H:i:s');
		
		$xml = new DomDocument();
		$xml->load('../blog.xml');
		
		$posts 				   		 = $xml->getElementsByTagName('posts')->item(0);
		$idComentario 				 = $xml->getElementsByTagName('comentario');

		foreach($idComentario as $Id){
			$ultimoId = $Id->getAttribute('idComentario') + 1;
		}
		$comentarios 		  		 = $posts->appendChild($xml->createElement('comentario'));
		$comentarioAttrId 	   		 = $xml->createAttribute('idComentario');
		$comentarioAttrIdPost        = $xml->createAttribute('idPostComentario');
		$comentarioAttrId->value     = $ultimoId;
		$comentarioAttrIdPost->value = $idPost;
		$comentarios->appendChild($comentarioAttrId);
		$comentarios->appendChild($comentarioAttrIdPost);
		$comentarios->appendChild($xml->createElement('autor',$usuario));
		$comentarios->appendChild($xml->createElement('conteudo',$conteudo));
		$comentarios->appendChild($xml->createElement('data', $data));
		if($xml->save('../blog.xml')){
			echo "<script>alert('Operaoção realizada com sucesso!');</script>";
		}
	}

?>

<body>
<h1>Criar Comentario</h1>
	<form action="criarComentario.php" method="post">
		Titulo Post:
		<br />
		<select name="optTitulo" id="optTitulo">
			<?php	
			  $xml = new DomDocument();
			  $xml->load('../blog.xml');
			
			  $posts = $xml->getElementsByTagName('post');
			
			  foreach($posts as $post){
				$idPost = $post->getAttribute('idPost');
				$titulo = $post->getElementsByTagName('titulo')->item(0)->nodeValue;
				
				echo "<option value={$idPost}>{$titulo}</option>";
			  }
			?>
	   </select>
	   <br />
	   <br />
	   Comentario:
	   <br />
	   <textarea cols="100" rows="20" id="txtComentario" name="txtComentario"></textarea>
	   <br />
	   <br />
	   <input type="submit" name="btnEnviar" id="btnEviar" value="Comentar" />
	   <br />
	</form>
	<a href="index.php">Voltar</a>
</body>

</html>